package search;

public class secondAbsMax {
    public static int find(int[] arr){
        int max = arr[0];
        int secondMax = arr[0];
        for (int e : arr) {
            if (Math.abs(e) > Math.abs(max)) {
                int tmp = max;
                max = e;
                secondMax = tmp;
            } else if (Math.abs(e) == Math.abs(max)) {
                max = e;
            } else if (Math.abs(e) > Math.abs(secondMax))
                secondMax = e;
        }
        return secondMax;
    }

    public static void main(String[] args) {
        System.out.println(find(new int[]{1,-2,3,2,-5}));
        System.out.println(find(new int[]{1,-2,3,-5,-5}));
    }
}

